package com.example.q_kang.hhh.home;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;
import com.sskj.base.MVPActivity;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.bean.HomeCommonBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.SPUtils;
import com.sskj.base.view.ToolBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class SellGamActivity extends MVPActivity<SellGamPresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.tv_zhongjie)
    TextView tvZhongjie;
    @BindView(R.id.cons0)
    ConstraintLayout cons0;
    @BindView(R.id.tv_lirun)
    TextView tvLirun;
    @BindView(R.id.cons1)
    ConstraintLayout cons1;
    @BindView(R.id.tv_chengben)
    TextView tvChengben;
    @BindView(R.id.cons2)
    ConstraintLayout cons2;
    @BindView(R.id.tv_fenhong)
    TextView tvFenhong;
    @BindView(R.id.cons3)
    ConstraintLayout cons3;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.cb_bank)
    CheckBox cb_bank;
    @BindView(R.id.cb_alipay)
    CheckBox cb_alipay;
    @BindView(R.id.cb_wechat)
    CheckBox cb_wechat;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_static_low)
    TextView tvStaticLow;
    @BindView(R.id.tv_static_per)
    TextView tvStaticPer;
    @BindView(R.id.tv_move_low)
    TextView tvMoveLow;
    @BindView(R.id.tv_move_per)
    TextView tvMovePer;
    @BindView(R.id.tv_money_low)
    TextView tvMoneyLow;
    @BindView(R.id.tv_money)
    TextView tvMoney;

    private int money_id = 7;
    private List<String> payWays = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.activity_sell_gam;
    }

    @Override
    public SellGamPresenter getPresenter() {
        return new SellGamPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, cons0, view -> {
            diamondChange(0);
            money_id = 7;
        });
        ClickUtil.click(300, cons1, view -> {
            diamondChange(1);
            money_id = 8;
        });
        ClickUtil.click(300, cons2, view -> {
            diamondChange(2);
            money_id = 9;
        });
        ClickUtil.click(300, cons3, view -> {
            diamondChange(3);
            money_id = 11;
        });

//        cb_bank.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.BANK, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindbank));
//                cb_bank.setChecked(false);
//            }
//        });
//        cb_alipay.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.ALIPAY, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindalipay));
//                cb_alipay.setChecked(false);
//            }
//        });
//        cb_wechat.setOnCheckedChangeListener((compoundButton, b) -> {
//            if (!SPUtils.get(App.INSTANCE, SpConfig.WECHAT, "").equals("1")) {
//                ToastUtils.show(App.INSTANCE.getString(R.string.bindwechat));
//                cb_wechat.setChecked(false);
//            }
//        });
        ClickUtil.click(300, btnSubmit, view -> {
            payWays.clear();
            if (TextUtils.isEmpty(getEtext(etMoney))) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSellGamActivity1));
                return;
            }

            if (!cb_bank.isChecked() && !cb_alipay.isChecked() && !cb_wechat.isChecked()) {
                ToastUtils.show(App.INSTANCE.getString(R.string.appJavaSellGamActivity2));
                return;
            }
            if (cb_bank.isChecked()) {
                payWays.add("bank");
            }
            if (cb_alipay.isChecked()) {
                payWays.add("zfb");
            }
            if (cb_wechat.isChecked()) {
                payWays.add("wx");
            }
            Gson gson = new Gson();
            String s = gson.toJson(payWays);
            String s1 = s.replaceAll("\"", "");
            String substring = s1.substring(1, s1.length() - 1);
            if (TextUtils.isEmpty(getEtext(etPwd))) {
                ToastUtils.show("请输入二级密码");
                return;
            }
            mPresenter.sellGam(money_id,
                    getEtext(etMoney),
                    substring, getEtext(etPwd));

        });
    }

    @Override
    public void initData() {
        mPresenter.getHomeDate();

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SellGamActivity.class);
        context.startActivity(intent);
    }

    private void diamondChange(int position) {
        cons0.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons1.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons2.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        cons3.setBackgroundResource(R.drawable.bg_stroke_gray_corner);
        switch (position) {
            case 0:
                cons0.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 1:
                cons1.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 2:
                cons2.setBackgroundResource(R.drawable.bg_list_blue);
                break;
            case 3:
                cons3.setBackgroundResource(R.drawable.bg_list_blue);
                break;
        }
    }

    public void getHomeDateSuccess(HomeCommonBean data) {
        if (data == null || data.getLevelInfo() == null || data.getUserMoney() == null) {
            return;
        }
        tvZhongjie.setText(data.getUserMoney().getSeven());
        tvLirun.setText(data.getUserMoney().getEight());
        tvChengben.setText(data.getUserMoney().getNine());
        tvFenhong.setText(data.getUserMoney().getEleven());
        tvStaticLow.setText(data.getLevelInfo().getStatic_low());
        tvStaticPer.setText(data.getLevelInfo().getStatic_per() + App.INSTANCE.getString(R.string.appXmlactivity_sell_gam338));
        tvMoveLow.setText(data.getLevelInfo().getMove_low());
        tvMovePer.setText(data.getLevelInfo().getMove_per() + App.INSTANCE.getString(R.string.appXmlactivity_sell_gam338));
        tvMoneyLow.setText(data.getLevelInfo().getMoney_low());
        tvMoney.setText(data.getLevelInfo().getMoney_per() + App.INSTANCE.getString(R.string.appXmlactivity_sell_gam407) + data.getLevelInfo().getMoney_out());
    }
}
